// components/popup/popup.js
import user from '../../../../utils/user.js'
import global from '../../../../utils/global'
import {
    inputchange,
    toast
} from '../../../../utils/util.js'
import {
    getUserAvatar,
    addInvitationMember,
    encryptedPhoneData
} from '../../../../api/app.js'
const app = getApp()
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        invite: Object,
    },

    /**
     * 组件的初始数据
     */
    data: {
        user: {
            name: '',
            position: '',
            phone: ''
        },
        showAuthUserStatus: true,
        showAuthPhoneStatus: false,
        version: ''
    },
    ready() {

        this.setData({
            version: user.system.version
        })
        if (!user.phoneNum) {
            this.setData({
                showAuthPhoneStatus: true,
            })
        } else {
            this.setData({
                ['user.phone']: user.phoneNum
            })
        }
        getUserAvatar({
            userId: this.data.invite.userId
        }).then(res => {
            console.log(res, 'resres')
            this.setData({
                avatarUrl: 'data:image/jpeg;base64,' + res
            })
        })
    },
    /**
     * 组件的方法列表
     */
    methods: {
        // 获取用户信息
        onGitUserInfo() {
            wx.getUserProfile({
                lang: 'zh_CN',
                desc: '向团队成员展示头像信息',
                success: (res) => {
                    console.log(res)
                    user.avatarUrl = res.userInfo.avatarUrl
                    if (!this.data.user.name) {
                        toast('请输入姓名!')
                        return 0
                    }
                    if (!this.data.user.position) {
                        toast('请输入职位!')
                        return 0
                    }
                    this.register()
                },
                fail(err) {
                    console.log(err)
                }
            })
        },
        // 获取用户两项授权
        auth() {
            // 获取用户信息授权
            this.setData({
                showAuthUserStatus: true
            })
            // 获取用户手机号授权
            this.setData({
                showAuthPhoneStatus: true
            })
        },
        // 完成用户授权
        authUserComplete() {
            this.setData({
                showAuthUserStatus: false
            })
        },
        // 完成手机授权
        authPhoneComplete() {
            this.setData({
                showAuthPhoneStatus: false
            })
            this.setData({
                'user.phone': user.phoneNum,
            })
        },
        submit(event) {
            console.log(event, 'submit')
            if (!this.data.user.name) {
                toast('请输入姓名!')
                return 0
            }
            if (!this.data.user.position) {
                toast('请输入职位!')
                return 0
            }
            wx.uploadFile({
                url: global.baseUrl + '/api/v1/pay/wesure-report-case/upload',
                filePath: event.detail.avatarUrl,
                name: 'file',
                formData: {
                    usePath: '1'
                },
                success: (res) => {
                    console.log(JSON.parse(res.data).data.fileUrl, '上传成功')
                    user.avatarUrl = JSON.parse(res.data).data.fileUrl
                    this.register()
                }
            })

        },
        register() {
            addInvitationMember({
                companyId: this.data.invite.companyId,
                userHeadUrl: user.avatarUrl,
                openId: user.openid,
                userName: this.data.user.name,
                phone: user.phoneNum,
                position: this.data.user.position,
                createUserId: this.data.invite.userId
            }).then(() => {
                app.toast('注册成功！')
                this.triggerEvent('complete')
            })
        },
        inputchange(event) {
            inputchange(event, this)
        },
        // 获取手机号的回调
        getPhoneNumber(res) {
            console.log(res, 'res1111111111')
            if (!res.detail.cloudID) {
                return
            }
            const data = {
                appId: app.globalData.appId,
                encryptedData: res.detail.encryptedData,
                sessionKey: user.session_key,
                iv: res.detail.iv
            }
            encryptedPhoneData(data).then(res => {
                user.phoneNum = res.phoneNumber
                this.setData({
                    ['user.phone']: res.phoneNumber
                })
                app.toast('授权成功')
                return
                // this.triggerEvent('complete', res.phoneNumber)
            })
        }
    }
})